const Database = require('better-sqlite3');
const db = new Database('app.db');

db.exec(`
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    login_name CHAR(20) NOT NULL UNIQUE, -- 添加 UNIQUE 约束
    password CHAR(20) DEFAULT 'ddd'
);

CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    text VARCHAR(1024) NOT NULL,
    user_id INTEGER NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id)
);
`);

// 在插入之前检查用户是否存在
const stmt = db.prepare("INSERT INTO users (login_name, password) VALUES (?, ?) ON CONFLICT(login_name) DO NOTHING;");
stmt.run('admin', 'ddd');

console.log('Database and initial data created successfully.');
db.close();